﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Student_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PaperId.Text = "试卷ID：" + Request.QueryString["id"];
            StuId.Text = "学生Id：" + Session["id"].ToString();
            initData();
        }

    }
    public void initData()
    {
        SqlConnection conn1 = new SqlConnection("Data Source=XUESHISHASHA;Initial Catalog=onlineexam;Persist Security Info=True;User ID=sa;Password=099623");
        
        //填空题
        string sql_1 = "SELECT t_p_fillinblanks.paperId, t_p_fillinblanks.fillInBlankId, t_paper.id, t_fillinblank.id AS fillinblankId, t_fillinblank.title, t_fillinblank.answer, t_fillinblank.score,t_fillinblank.analysis, t_fillinblank.hardLevel FROM t_p_fillinblanks INNER JOIN t_fillinblank ON t_p_fillinblanks.fillInBlankId = t_fillinblank.id INNER JOIN t_paper ON t_paper.id='" + Request.QueryString["id"] + "' and t_p_fillinblanks.paperId = t_paper.id";
        SqlDataAdapter adapter_1 = new SqlDataAdapter(sql_1, conn1);
        DataSet ds_1 = new DataSet();
        adapter_1.Fill(ds_1,"fillInBlank");
        GridView1.DataSource = ds_1.Tables["fillInBlank"].DefaultView;
        GridView1.DataBind();

        SqlConnection conn2 = new SqlConnection("Data Source=XUESHISHASHA;Initial Catalog=onlineexam;Persist Security Info=True;User ID=sa;Password=099623");
        
        //单选
        string sql_2 = "SELECT t_exclusivechoice.title, t_exclusivechoice.answer, t_exclusivechoice.hardLevel, t_exclusivechoice.score, t_exclusivechoice.choiceA, t_exclusivechoice.choiceB, t_exclusivechoice.choiceC, t_exclusivechoice.choiceD, t_exclusivechoice.analysis FROM t_p_exclusivechoices INNER JOIN t_exclusivechoice ON t_p_exclusivechoices.exclusiveChoiceId = t_exclusivechoice.id INNER JOIN t_paper ON t_paper.id='" + Request.QueryString["id"] + "' and t_p_exclusivechoices.paperId = t_paper.id";
        SqlDataAdapter adapter_2 = new SqlDataAdapter(sql_2, conn2);
        DataSet ds_2 = new DataSet();
        adapter_2.Fill(ds_2, "exclusivechoice");
        GridView2.DataSource = ds_2.Tables["exclusivechoice"].DefaultView;
        GridView2.DataBind();

        SqlConnection conn3 = new SqlConnection("Data Source=XUESHISHASHA;Initial Catalog=onlineexam;Persist Security Info=True;User ID=sa;Password=099623");

        //多选
        string sql_3 = "SELECT t_multiplechoice.title, t_multiplechoice.answer, t_multiplechoice.hardLevel, t_multiplechoice.score, t_multiplechoice.analysis, t_multiplechoice.choiceA, t_multiplechoice.choiceB, t_multiplechoice.choiceC, t_multiplechoice.choiceD, t_multiplechoice.choiceE, t_multiplechoice.choiceF, t_multiplechoice.choiceNum, t_paper.id, t_p_multiplechoices.paperId, t_p_multiplechoices.multipleChoiceId, t_multiplechoice.id AS Expr1 FROM t_p_multiplechoices INNER JOIN t_paper ON t_paper.id='" + Request.QueryString["id"] + "' and  t_p_multiplechoices.paperId = t_paper.id INNER JOIN t_multiplechoice ON t_p_multiplechoices.multipleChoiceId = t_multiplechoice.id";
        SqlDataAdapter adapter_3 = new SqlDataAdapter(sql_3, conn3);
        DataSet ds_3 = new DataSet();
        adapter_3.Fill(ds_3, "multiplechoice");
        GridView3.DataSource = ds_3.Tables["multiplechoice"].DefaultView;
        GridView3.DataBind();

        SqlConnection conn4 = new SqlConnection("Data Source=XUESHISHASHA;Initial Catalog=onlineexam;Persist Security Info=True;User ID=sa;Password=099623");

        //多选
        string sql_4 = "SELECT t_shortanswer.answer, t_shortanswer.hardLevel, t_shortanswer.score, t_shortanswer.title, t_shortanswer.analysis FROM t_p_shortanswers INNER JOIN t_paper ON t_paper.id='" + Request.QueryString["id"] + "' and t_p_shortanswers.paperId = t_paper.id INNER JOIN t_shortanswer ON t_p_shortanswers.shortAnswerId = t_shortanswer.id";
        SqlDataAdapter adapter_4 = new SqlDataAdapter(sql_4, conn4);
        DataSet ds_4 = new DataSet();
        adapter_4.Fill(ds_4, "shortanswer");
        GridView4.DataSource = ds_4.Tables["shortanswer"].DefaultView;
        GridView4.DataBind();


    }
    protected void SubmitPaperClick(object sender, EventArgs e)
    {
        msg.Text = "SubmitPaperClick";
        msg.Text = "SubmitPaperClick";
        msg.Text = "SubmitPaperClick";
        msg.Text = "SubmitPaperClick";
        msg.Text = "SubmitPaperClick";
        //msg.Text = "Hello this is the server Click!";
        //Response.Write("<script language='javascript'>alert('您太棒了！您的成绩为：123分！')</script>");
    }
    protected void btn_Click(object sender, EventArgs e)
    {       
        msg.Text = "本次考试统计：";
        //int singlemark = int.Parse(((Label)GridView1.Rows[0].FindControl("Label4")).Text);//取出单选题的每题分值

        int stuTiankongScore = 0;

        foreach (GridViewRow dr in GridView1.Rows)//对填空题每题进行判断用户选择答案
        {

            string userAnswer = ((TextBox)(dr.FindControl("answer"))).Text;  
            string answer = ((Label)(dr.FindControl("answerLabel"))).Text;            
            int score = Int32.Parse(((Label)(dr.FindControl("scoreLabel"))).Text);

            //msg.Text += userAnswer + "," + answer;

            if (userAnswer.Trim().Equals(answer.Trim()))
            {
                //msg.Text += "相等  ";
                //msg.Text += score.ToString();
                stuTiankongScore += score;
            }
            else
            {
                //msg.Text += "不相等";
            }
        }


        //获得单选成绩
        int stuDanxuanScore=0;

        foreach (GridViewRow dr in GridView2.Rows)//对单选题每题进行判断用户选择答案
        {
            
            RadioButtonList list = (RadioButtonList)(dr.FindControl("RadioButtonList2"));
            string answer = ((Label)(dr.FindControl("answerLabel"))).Text;
            answer = answer.Substring(answer.Length - 1);
            int score = Int32.Parse(((Label)(dr.FindControl("scoreLabel"))).Text);
            
            if (list.SelectedValue.Equals(answer))
            {
                //msg.Text += "相等  ";
                //msg.Text += score.ToString();
                stuDanxuanScore +=score;                 
            }
            else
            {
                //msg.Text += "不相等";
            }
        }

        //多选题
        int stuDuoxuanScore = 0;

        foreach (GridViewRow dr in GridView3.Rows)//对单选题每题进行判断用户选择答案
        {

            CheckBoxList list = (CheckBoxList)(dr.FindControl("CheckBoxList1"));

            string userAnswerString="";
            foreach (ListItem li in list.Items)
            {
                if (li.Selected == true)
                {
                    userAnswerString += li.Text;
                    
                }
            }

            string answer = ((Label)(dr.FindControl("answerLabel"))).Text;
            
            int score = Int32.Parse(((Label)(dr.FindControl("scoreLabel"))).Text);
            //答案正确
            if(userAnswerString.Trim().Equals(answer.Trim()))
            {
                //msg.Text += score.ToString();
                stuDuoxuanScore += score;
            }

            //msg.Text += "<br/>userAnswerString:" + userAnswerString + ".<br/>";
            //msg.Text += "<br/>answer:" + answer + ".<br/>";
            //msg.Text += "<br/>score:" + score + ".<br/>";
        }


        //简答题
        double stuJiandaScore = 0;

        foreach (GridViewRow dr in GridView4.Rows)//对单选题每题进行判断用户选择答案
        {

            string userAnswer = ((TextBox)(dr.FindControl("answer"))).Text;

            string answer = ((Label)(dr.FindControl("answerLabel"))).Text;

            string[] arrAnswer = answer.Split(',');

            int score = Int32.Parse(((Label)(dr.FindControl("scoreLabel"))).Text);

            double count = (double)(arrAnswer.Length);

            //答案正确
            for(int i=0;i<count;i++)
            {
                if (userAnswer.Contains(arrAnswer[i]))
                {
                   // msg.Text += score.ToString();
                    stuJiandaScore += score / count;
                }
            }


            //msg.Text += "<br/>userAnswer:" + userAnswer + ".<br/>";
            //msg.Text += "<br/>answer:" + answer + ".<br/>";
            //msg.Text += "<br/>score:" + score + ".<br/>";
        }


        msg.Text += "<br/>单选总分：" + stuDanxuanScore;
        msg.Text += "<br/>填空总分：" + stuTiankongScore;
        msg.Text += "<br/>多选总分：" + stuDuoxuanScore;
        msg.Text += "<br/>简答总分：" + stuJiandaScore;
        msg.Text += "<br/><font color='blue'>总分：" + stuJiandaScore+"</font>";
        

    }
}
